<template>
   <view>
      <button @click="login">登入</button>
   </view>
</template>

<script>
   import env from '@/env';
   export default {
      data() {
         return {
            
         }
      },
      onLoad() {
      },
      methods: {
         async login() {
            Promise.all([this.getCode(), this.getUserInfo()]).then(res => {
               // 3-把code 昵称 头像 封装成data对象
               let user = {
                  code: res[0].code,
                  name: res[1].userInfo.nickName,
                  avatar: res[1].userInfo.avatarUrl
               }
               
               uni.request({  
                  url: 'https://api.weixin.qq.com/sns/jscode2session',  
                  method:'GET',  
                  data: {  
                     appid: 'wx616a1663bc6b5f26',
                     secret: '92d560100960af3a9a671fd19d0f72b8',  
                     js_code: res[0].code, //wx.login 登录成功后的code  
                     grant_type: 'authorization_code',
                  },  
                  success: (result) => {  
                     // 换取成功后 暂存这些数据 留作后续操作  
                     let openId = result.data.openid; //openid 用户唯一标识  
                     user['id'] = openId;
                     
                     console.log(openId);
                     uni.setStorageSync('TOKEN', openId);
                     uni.setStorageSync('USER', user);
                    
                     // 更新用户信息
                     uni.request({
                        url:  env.BASE_URI +  '/user',
                        method: 'POST',
                        data: user,
                        success: (res) => {
                           if(res) {
                              console.log(user['name'] + '用户登录成功');
                           }
                        }
                     });
                     
                     uni.reLaunch({
                        url: '/pages/index/index',
                        success() {
                           // #ifdef APP-PLUS
                           plus.navigator.closeSplashscreen();
                           // #endif
                        }
                     });
                  }  
               }); 
            })
         },
         // 1-获取code
         getCode() {
            return new Promise((resolve, reject) => {
               wx.login({
                  timeout: 5000,
                  success: (res) => {
                     resolve(res)
                  },
                  fail: (err) => {
                     reject(err)
                  }
               })
            });
         },
         // 2-获取头像和昵称
         getUserInfo() {
            return new Promise((resolve, reject) => {
               wx.getUserProfile({
                  desc: "获取用户信息",
                  success: (res) => {
                     resolve(res)
                  },
                  fail: (err) => {
                     reject(err)
                  }
               })
            });
         }
      }
   }
</script>

<style>

</style>
